﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Data.Entity.Validation;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SH.DataAccess;
using SH.DataAccess.Migrations;
using SH.Model;

namespace SH.Console
{
    class Program
    {
        static void Main(string[] args)
        {
            // установка соединения
            Database.SetInitializer(new MigrateDatabaseToLatestVersion<ModelContainer, Configuration>());
            var context = new ModelContainer();
            context.Database.Initialize(false);

            // добавление данных
            context.Database.Initialize(true);
            // todo: here I'll cause error validations
            var st = new Status { Name = "e@##6675436453645444444444444444444444444444444444443645364fgdgdfg43543543fr43543r43f34f43f43f435366###############@rwe" };

            context.Statuses.Add(st);

            try
            {
                context.SaveChanges();
            }
            catch (DbEntityValidationException e)
            {
                foreach (var x in e.EntityValidationErrors)
                {
                    foreach (var y in x.ValidationErrors)
                    {
                        // todo: it's work, so I'm got validation message
                        System.Console.WriteLine("{0}.{1}: {2}", x.Entry.Entity.GetType(), y.PropertyName, y.ErrorMessage);
                    }
                }
            }

            



            // вывод данных
            var sh = from status in context.Statuses
                     where status.Id != 0
                     select new { Id = status.Id, Name = status.Name };
            foreach (var v in sh)
            {
                System.Console.WriteLine("id={0}, name={1}", v.Id, v.Name);
            }

            System.Console.ReadKey();
        }
    }
}
